<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------
quanli_require('module/base/billHandler.class.php');
quanli_require('module/base/xShopHandler.class.php');
quanli_include('framework/phpExecl/PHPExcel.php');
quanli_include('framework/phpExecl/PHPExcel/Writer/Excel2007.php');

/**
 * 物流异常订单预警
 *
 */
class pa_billWarning {
    public function execute() {
        if (isset($_REQUEST['operFlg'])) {
            switch($_REQUEST['operFlg']) {
                case 1: //订单物流统计
                    $this->billExpressInfo(); 
                    break;
	           	case 2: //订单物流导出
	                $this->billWarningExport(); 
	                break;
	            case 3: //订单物流列表
	                $this->billWarningList();
	                break;
	            default:
	            	$this->toError('参数错误') ;
	            	break ;
            }
        }
    }
    
     /**
     * 转跳到错误页
     * @param string $msg 错误信息
     */
    private function toError( $msg ) {
        global $G_PGLOB;
        $G_PGLOB->display_error_page( $msg );
    }
    
    //订单物流统计页面
	private function billExpressInfo() {
		global $G_PGLOB;
		
		$xShopHandler = new xShopHandler($G_PGLOB->DBCA) ;
		$channelArray = $xShopHandler->getChannel() ;
		
		//总待收货的订单
		$totalBill_1 = $G_PGLOB->DBCA->stmt_query('SELECT COUNT(*) count FROM `bill_logistics_warning`') ; 
	
		//总异常订单
		$totalBill_2 = $G_PGLOB->DBCA->stmt_query('SELECT COUNT(*) count FROM `bill_logistics_warning` WHERE  freight_state=3') ;
		
		//总拒收订单数目
		$totalBill_3 = $G_PGLOB->DBCA->stmt_query('SELECT COUNT(*) count FROM `bill_logistics_warning` WHERE  freight_state=4') ;
		
		//总无流转订单数目
		$totalBill_4 = $G_PGLOB->DBCA->stmt_query('SELECT COUNT(*) count FROM `bill_logistics_warning` WHERE  freight_state=1') ;
		
		if($channelArray){
			foreach($channelArray as &$c_l){
				//渠道待收货的订单
				$bill_1 = $G_PGLOB->DBCA->stmt_query('SELECT COUNT(*) count FROM `bill_logistics_warning` WHERE channel=?' , 'i' , $c_l->channelID) ; 
				$c_l->bill_1 = $bill_1[0]->count ;
				
				//渠道异常的订单
				$bill_2 = $G_PGLOB->DBCA->stmt_query('SELECT COUNT(*) count FROM `bill_logistics_warning` WHERE  freight_state=3 AND channel=?' , 'i' , $c_l->channelID) ;
				$c_l->bill_2 = $bill_2[0]->count ;
				
				//渠道拒收的订单
				$bill_3 = $G_PGLOB->DBCA->stmt_query('SELECT COUNT(*) count FROM `bill_logistics_warning` WHERE  freight_state=4 AND channel=?' , 'i' , $c_l->channelID) ;
				$c_l->bill_3 = $bill_3[0]->count ;
				
				//无流转的订单
				$bill_4 = $G_PGLOB->DBCA->stmt_query('SELECT COUNT(*) count FROM `bill_logistics_warning` WHERE  freight_state=1 AND channel=?' , 'i' , $c_l->channelID) ;
				$c_l->bill_4 = $bill_4[0]->count ;
			}
		}
		
		//组织页面参数
		$billExpressArr = array('totalBill_1' => $totalBill_1[0]->count ,
								'totalBill_2' => $totalBill_2[0]->count ,
								'totalBill_3' => $totalBill_3[0]->count ,
								'totalBill_4' => $totalBill_4[0]->count ,
								'channelArr'  => $channelArray
								) ;
		
		$G_PGLOB->smarty->assign_by_ref('billExpressArr' , $billExpressArr) ;
		$G_PGLOB->smarty->display('bill/billExpressInfo.tpl') ;
	}
	
	//异常物流订单列表
    private function billWarningList() {
        global $G_PGLOB;
        
        $channel = isset($_REQUEST['channel']) ?  $_REQUEST['channel'] : '' ;						//渠道
        $freight_state = isset($_REQUEST['freight_state']) ?  $_REQUEST['freight_state'] : '' ;		//物流状态
        
        //查询渠道信息
        $xShopHandler = new xShopHandler($G_PGLOB->DBCA) ;
		$channelArray = $xShopHandler->getChannel() ;
		
		$G_PGLOB->smarty->assign_by_ref('channel' , $channelArray) ;
		
		$G_PGLOB->smarty->assign('channelID' , $channel) ;
		$G_PGLOB->smarty->assign('freight_state' , $freight_state) ;
        $G_PGLOB->smarty->display('bill/billWarning.tpl') ;
    }
    
    //订单数据导出
	private function billWarningExport() {
		global $G_PGLOB;
		
		$_REQUEST['rp'] = 0 ;
		$billHandler = new billHandler($G_PGLOB->DBCA) ;
		$billArray   = $billHandler->searchBillWarning($_REQUEST) ;
		
		//实例excel类
		$objPHPExcel = new PHPExcel();
		
 		//======设置excel的属性
 
		//创建人
		$objPHPExcel->getProperties()->setCreator("$G_PGLOB->currentUserName");
		//最后修改人
		$objPHPExcel->getProperties()->setLastModifiedBy("$G_PGLOB->currentUserName");
		//标题
		$objPHPExcel->getProperties()->setTitle("导出异常订单物流");
		//题目
		$objPHPExcel->getProperties()->setSubject("导出异常订单物流");
		//描述
		$objPHPExcel->getProperties()->setDescription("导出异常订单物流");
		
		//设置当前的sheet
		$objPHPExcel->setActiveSheetIndex(0);
		//设置sheet的name
		$objPHPExcel->getActiveSheet()->setTitle('导出异常订单物流');
		
		//设置标题
		$objPHPExcel->getActiveSheet()->setCellValue('A1', '订单号');
		$objPHPExcel->getActiveSheet()->setCellValue('B1', '收货人');
		$objPHPExcel->getActiveSheet()->setCellValue('C1', '手机号');
		$objPHPExcel->getActiveSheet()->setCellValue('D1', '收货地址');
		$objPHPExcel->getActiveSheet()->setCellValue('E1', '快递公司');
		$objPHPExcel->getActiveSheet()->setCellValue('F1', '快递单号');
		$objPHPExcel->getActiveSheet()->setCellValue('G1', '物流状态');
		$objPHPExcel->getActiveSheet()->setCellValue('H1', '异常原因');
		
		if ($billArray) {
			$i = 2;
			foreach($billArray as &$b_l) {
				$freight_state = '';
            	if($b_l->freight_state == 0)
            		$freight_state = '在途';
            	elseif($b_l->freight_state == 1)
            		$freight_state = '揽件';
            	elseif($b_l->freight_state == 2)	
            		$freight_state = '疑难';
            	elseif($b_l->freight_state == 3)		
            		$freight_state = '签收';
            	elseif($b_l->freight_state == 4)		
            		$freight_state = '退签';	
            	elseif($b_l->freight_state == 5)		
            		$freight_state = '派件';	
            	elseif($b_l->freight_state == 6)		
            		$freight_state = '退回';	
				
				$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $b_l->bill_no . "\t");
				$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, decryptD($b_l->deliver_name));
				$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, decryptD($b_l->deliver_mobile));
				$objPHPExcel->getActiveSheet()->setCellValue('D' . $i, decryptD($b_l->deliver_address));
				$objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $b_l->freight_company_name);
				$objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $b_l->freight_bill_no);
				$objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $freight_state);
				$objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $b_l->context);
				$i++;
			}
		}
		
		//设定字段宽度
		$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(40);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
		$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
		$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
		$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(40);
		
		//文件名称
		$fileName = '导出异常订单物流_' . date('YmdHis') . '.xlsx';
		
		$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
		header("Content-Type: application/force-download");
		header("Content-Disposition:inline;filename=$fileName");
		header("Content-Transfer-Encoding: binary");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Pragma: no-cache");
		$objWriter->save('php://output');
	}
}
